# coding:utf-8
"""
description:Schedule页面
"""
import os
import random
import time, datetime

from appium.webdriver.common import mobileby

from common.Gesture_Mainpulation import gesture_mainpulation
from common.Get_Yaml import get_yaml
from common.Log import LOG, logger
from config.GlobalParameter import TEST_Element_YAML
from pages.Devices.ModeSchedule_page import ModeSchedulePublic_page

Data = get_yaml(os.path.join(TEST_Element_YAML, 'Devices', 'Schedule.yaml'))


class Schedule_page(ModeSchedulePublic_page):
    by = mobileby.MobileBy()

    """添加schedule的save按钮"""
    AddScheduleSave_loc = (by.ID, Data.get_elementInfo(0))

    def click_AddScheduleSave(self):
        """添加schedule的save按钮"""
        self.find_element(*self.AddScheduleSave_loc).click()

    """dimmer，1-3 gang switch，high power，plug和ceiling light，添加schedule的on按钮"""
    btnAddScheduleActionOn_loc = (by.ID, Data.get_elementInfo(1))

    def click_btnAddScheduleOn(self):
        """dimmer，1-3 gang switch，high power，plug和ceiling light，添加schedule的on按钮"""
        self.find_element(*self.btnAddScheduleActionOn_loc).click()

    """dimmer，1-3 gang switch，high power，plug和ceiling light，添加schedule的off按钮"""
    btnAddScheduleActionOff_loc = (by.ID, Data.get_elementInfo(2))

    def click_btnAddScheduleOff(self):
        """dimmer，1-3 gang switch，high power，plug和ceiling light，添加schedule的off按钮"""
        self.find_element(*self.btnAddScheduleActionOff_loc).click()

    """添加Device Schedule用。点击Sunday, Monday, Tuesday, Wednesday, Thursday, 
    Friday, Saturday后，分别显示once、weekends、 weekdays、everyday。"""
    AddScheduleRepeat_loc = (by.ID, Data.get_elementInfo(3))
    """Sunday"""
    AddScheduleRepeat1_loc = (by.ID, Data.get_elementInfo(4))
    """Monday"""
    AddScheduleRepeat2_loc = (by.ID, Data.get_elementInfo(5))
    """Tuesday"""
    AddScheduleRepeat3_loc = (by.ID, Data.get_elementInfo(6))
    """Wednesday"""
    AddScheduleRepeat4_loc = (by.ID, Data.get_elementInfo(7))
    """Thurday"""
    AddScheduleRepeat5_loc = (by.ID, Data.get_elementInfo(8))
    """Friday"""
    AddScheduleRepeat6_loc = (by.ID, Data.get_elementInfo(9))
    """Saturday"""
    AddScheduleRepeat7_loc = (by.ID, Data.get_elementInfo(10))

    def get_AddScheduleRepeat(self):
        """添加页面，获取repeat选中的后文字显示"""
        return self.find_element(*self.AddScheduleRepeat_loc).text

    def click_AddScheduleRepeat(self, times):
        """
        根据点击几次，来随机选择日期
        :param times:点击次数
        :return:
        """
        for i in range(times):
            nums = random.randint(1, 7)
            if nums == 1:
                LOG.info("随机选择Add Schedule 页面日期,Sunday...")
                self.find_element(*self.AddScheduleRepeat1_loc).click()
            elif nums == 2:
                LOG.info("随机选择Add Schedule 页面日期,Monday...")
                self.find_element(*self.AddScheduleRepeat2_loc).click()
            elif nums == 3:
                LOG.info("随机选择Add Schedule 页面日期,Tuesday...")
                self.find_element(*self.AddScheduleRepeat3_loc).click()
            elif nums == 4:
                LOG.info("随机选择Add Schedule 页面日期,Wednesday...")
                self.find_element(*self.AddScheduleRepeat4_loc).click()
            elif nums == 5:
                LOG.info("随机选择Add Schedule 页面日期,Thurday...")
                self.find_element(*self.AddScheduleRepeat5_loc).click()
            elif nums == 6:
                LOG.info("随机选择Add Schedule 页面日期,Friday...")
                self.find_element(*self.AddScheduleRepeat6_loc).click()
            else:
                LOG.info("随机选择Add Schedule 页面日期,Saturday...")
                self.find_element(*self.AddScheduleRepeat7_loc).click()

        """编辑schedule的save按钮"""

    EditScheduleSave_loc = (by.ID, Data.get_elementInfo(11))

    def click_EditScheduleSave(self):
        """编辑schedule的save按钮"""
        self.find_element(*self.EditScheduleSave_loc).click()

    """dimmer，1-3 gang switch，high power，plug和ceiling light，编辑schedule的on按钮"""
    btnEditScheduleActionOn_loc = (by.ID, Data.get_elementInfo(12))

    def click_btnEditScheduleOn(self):
        """dimmer，1-3 gang switch，high power，plug和ceiling light，编辑schedule的on按钮"""
        self.find_element(*self.btnEditScheduleActionOn_loc).click()

    """dimmer，1-3 gang switch，high power，plug和ceiling light，编辑schedule的off按钮"""
    btnEditScheduleActionOff_loc = (by.ID, Data.get_elementInfo(13))

    def click_btnEditScheduleOff(self):
        """dimmer，1-3 gang switch，high power，plug和ceiling light，编辑schedule的off按钮"""
        self.find_element(*self.btnEditScheduleActionOff_loc).click()

    """编辑Device Schedule用。点击Sunday, Monday, Tuesday, Wednesday, Thursday, 
    Friday, Saturday后，分别显示once、weekends、 weekdays、everyday。"""
    EditScheduleRepeat_loc = (by.ID, Data.get_elementInfo(14))
    """Sunday"""
    EditScheduleRepeat1_loc = (by.ID, Data.get_elementInfo(15))
    """Monday"""
    EditScheduleRepeat2_loc = (by.ID, Data.get_elementInfo(16))
    """Tuesday"""
    EditScheduleRepeat3_loc = (by.ID, Data.get_elementInfo(17))
    """Wednesday"""
    EditScheduleRepeat4_loc = (by.ID, Data.get_elementInfo(18))
    """Thurday"""
    EditScheduleRepeat5_loc = (by.ID, Data.get_elementInfo(19))
    """Friday"""
    EditScheduleRepeat6_loc = (by.ID, Data.get_elementInfo(20))
    """Saturday"""
    EditScheduleRepeat7_loc = (by.ID, Data.get_elementInfo(21))

    def get_EditScheduleRepeat(self):
        """获取repeat选中的后文字显示"""
        return self.find_element(*self.EditScheduleRepeat_loc).text

    """点击edit schedule page的delete按钮"""
    btnEditScheduleDelete_loc = (by.ID, Data.get_elementInfo(22))

    def click_btnEditScheduleDelete(self):
        """点击edit schedule page的delete按钮"""
        self.find_element(*self.btnEditScheduleDelete_loc).click()

    def click_EditScheduleRepeat(self, times):
        """
        根据点击几次，来随机选择日期
        :param times:点击次数
        :return:
        """
        for i in range(times):
            nums = random.randint(1, 7)
            if nums == 1:
                LOG.info("随机选择Edit Schedule 页面日期,Sunday...")
                self.find_element(*self.EditScheduleRepeat1_loc).click()
            elif nums == 2:
                LOG.info("随机选择Edit Schedule 页面日期,Monday...")
                self.find_element(*self.EditScheduleRepeat2_loc).click()
            elif nums == 3:
                LOG.info("随机选择Edit Schedule 页面日期,Tuesday...")
                self.find_element(*self.EditScheduleRepeat3_loc).click()
            elif nums == 4:
                LOG.info("随机选择Edit Schedule 页面日期,Wednesday...")
                self.find_element(*self.EditScheduleRepeat4_loc).click()
            elif nums == 5:
                LOG.info("随机选择Edit Schedule 页面日期,Thurday...")
                self.find_element(*self.EditScheduleRepeat5_loc).click()
            elif nums == 6:
                LOG.info("随机选择Edit Schedule 页面日期,Friday...")
                self.find_element(*self.EditScheduleRepeat6_loc).click()
            else:
                LOG.info("随机选择Edit Schedule 页面日期,Saturday...")
                self.find_element(*self.EditScheduleRepeat7_loc).click()

    def func_AddSchedule(self, message):
        """
        直接点击save保存成功
        :param message:
        :return:
        """
        self.click_btnDeviceSchedulingAdd()  # 点击Schedule page右上角+号
        self.click_AddScheduleSave()
        return self.find_toast(message)

    def func_AddSchedule2(self, times):
        """
        添加，随机选择power和repeat days，断言添加成功的数量
        :param times: 随机选择的repeat days
        :return:
        """
        num = len(super().get_ScheduleAction())  # 新增前的数量
        self.click_btnDeviceSchedulingAdd()  # 承接上面Add Schedule1方法，直接Schedule list右上角+号
        if random.randint(1, 2) == 1:
            LOG.info("添加Schedule，选择Power为Off。。。")
            self.click_btnAddScheduleOff()
        self.click_AddScheduleRepeat(times)  # 随机选择repeat
        self.click_AddScheduleSave()
        return num + 1 == len(super().get_ScheduleAction())

    def func_EditSchedule(self, times, message):
        """
        修改，随机选择power和repeat days，断言提示内容
        :param times:
        :param message:
        :return:
        """
        # 承接上面Add Schedule后，随机选择item进入edit page
        random.choice(super().get_ScheduleAction()).click()
        if random.randint(1, 2) == 1:
            LOG.info("修改Schedule，选择Power为Off。。。")
            self.click_btnEditScheduleOff()
        self.click_EditScheduleRepeat(times)  # 随机选择repeat
        self.click_EditScheduleSave()
        return self.find_toast(message)

    def func_EditSchedule2(self, condition):
        """
        只修改time》minute为下一分钟，设备执行后，检查Schedule page该项switch为off
        :return:
        """
        if super().assert_SchedulingAddedList():
            for index, row in enumerate(super().get_ScheduleAction()):
                row.click()  # 直接根据第一个进行编辑，跳转edit page
                # 先滑动minute，加1min。避免滑动后改动hour
                super().swipe_NumberPicker_Text(datetime.datetime.now().minute + 2, indexs=1)
                # 滑动hour
                super().swipe_NumberPicker_Text(datetime.datetime.now().hour)
                self.click_EditScheduleSave()
                if condition == super().get_ScheduleRepeat()[index].text:
                    time.sleep(120)  # 虽然选择+2 min，但重新选择hour会耗时间
                    super().swipe_listPage()
                    LOG.info("编辑ONCE的Schedule，等待120s后，关闭switch。实际为%s》》》" % super().get_switchSchedule()[index].text)
                    return super().get_switchSchedule()[index].text == "OFF"
                else:
                    time.sleep(120)
                    super().swipe_listPage()
                    LOG.info(
                        "编辑不是ONCE的Schedule，等待120s后，switch依然开启。实际为%s》》》" % super().get_switchSchedule()[index].text)
                    return super().get_switchSchedule()[index].text == "ON"
        else:
            LOG.info("未找到可编辑的Schedule项，请检查Schedule list page数据》》》")
            return False

    def func_DelSchedule(self, message):
        """
        在item左滑或者编辑页点击delete按钮进行删除操作
        :param message:
        :return:
        """
        super().click_ControlSchedule()  # 点击control page的Schedule icon

        if super().assert_SchedulingAddedList():
            for i in enumerate(super().get_switchSchedule()):
                if random.randint(1, 1) == 1:
                    LOG.info("删除操作。左滑动列表项点击delete按钮进行删除》》》")
                    super().leftSwipeClickDel(self.get_switchSchedule(), self.get_ScheduleTime())  # 随机选择item点击delete
                    super().click_confirm()
                else:
                    LOG.info("删除操作。进入列表项详情页点击底部delete按钮进行删除》》》")
                    random.choice(super().get_ScheduleAction()).click()  # 随机选择item进入edit page
                    self.click_btnEditScheduleDelete()
                    super().click_confirm()
                    self.find_toast(message)
            return super().assert_SchedulingAddedList()
        else:
            LOG.info("未找到可编辑的Schedule项，请检查Schedule list page数据》》》")
            return False
